package com.song.ssm.controller;

import com.song.ssm.pojo.Person;
import com.song.ssm.service.PersonService;
import jakarta.annotation.Resource;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/9/1
 * @Version 1.0
 **/
@Controller
public class HelloController {

    @Resource
    private PersonService personService;

    @GetMapping("/index")
    public String findAll(Model model) {
        model.addAttribute("list",this.personService.findAll());
        return "index";
    }

    @GetMapping("/upload")
    public String toupload(){
        return "upload";
    }
    @PostMapping("/upload")
    public String upload(MultipartFile pic, HttpServletRequest request){
        System.out.println(pic.getContentType());
        System.out.println(pic.getOriginalFilename());
        System.out.println(pic.getName());
        String realPath = request.getSession().getServletContext().getRealPath("/upload");
        System.out.println(realPath);
        File file = new File(realPath+File.separator+pic.getOriginalFilename());
        try {
            pic.transferTo(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return "";
    }
     @GetMapping("/list")
     @ResponseBody
     public List<Person> findAll() {
         return this.personService.findAll();
     }
}
